import requests




class weather_get:
    def __init__(self,city):
        self.weather="Sunny"
        self.city=city
        self.amap_key = '811c43c94610118d5b48e2a53232fb91'
        self.url_live = f'https://restapi.amap.com/v3/weather/weatherInfo?key={self.amap_key}&city={self.city}'
        self.url_all = f'https://restapi.amap.com/v3/weather/weatherInfo?key={self.amap_key}&city={self.city}&extensions=all'
        self.response = ""
    def get_weather(self):
        i=1
        """Get the current weather"""
        self.response_live = requests.get(self.url_live)
        self.response_all = requests.get(self.url_all)
        if self.response_live.status_code == 200:
        # 解析响应内容，通常返回的是JSON格式
            weather_data_live = self.response_live.json()
        else:
            if i<30:
                self.get_weather()
                i=i+1
        if weather_data_live:
            # print("yes")
            weather_data_live = weather_data_live['lives']



        if self.response_all.status_code == 200:
        # 解析响应内容，通常返回的是JSON格式
            weather_data_all = self.response_all.json()
            print(weather_data_all)
        else:
            if i<30:
                self.get_weather()
                i=i+1
        if weather_data_all:
            # print("yes")
            weather_data_all= weather_data_all['forecasts']
        print(weather_data_live)
        print(weather_data_all)
        self.weather_live=weather_data_live[0]['weather']
        self.weather_all=weather_data_all[0]['casts'][0]['dayweather']
        self.temp_live=weather_data_live[0]['temperature']
        self.temp_all=weather_data_all[0]['casts'][0]['daytemp']
        self.weather_all_two = weather_data_all[0]['casts'][1]['dayweather']
        self.temp_all_two = weather_data_all[0]['casts'][1]['daytemp']
        return self.weather_live,self.weather_all,self.temp_live,self.temp_all,self.weather_all_two,self.temp_all_two



#
# chaxun=weather_get("郑州")
# chaxun.get_weather()
#

# import pandas as pd
# import requests
#
#
# url='https://restapi.amap.com/v3/weather/weatherInfo?'
#
# parameters={
#     'key':'811c43c94610118d5b48e2a53232fb91',#（替换成你自己的key）
#     'city':"郑州"
# }
# res=requests.get(url,params=parameters)
# print(res)
# data=res.json()['lives']
# print(data)
